package com.whzm.web;

import com.whzm.pojo.vo.CUser;
import com.whzm.pojo.vo.FlowMatchVo;
import com.whzm.pojo.vo.PUser;
import com.whzm.service.Impl.PhoneServiceImpl;
import com.whzm.service.Impl.RedisServiceImpl;
import com.whzm.service.Impl.UserServiceImpl;
import com.whzm.service.Impl.WorkServiceImpl;
import com.whzm.util.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Random;

@RestController
public class UserController {
    @Autowired
    RedisServiceImpl redisService;

    @Autowired
    PhoneServiceImpl phoneService;

    @Autowired
    WorkServiceImpl workService;

    @Autowired
    UserServiceImpl userService;

    /**
     * 公司用户注册
     * @param cUser
     * @return
     */
    @PostMapping("/register/company")
    public ResponseEntity cRegister(@RequestBody CUser cUser){ return userService.addCompanyUser(cUser); }

    /**
     * 个人用户注册
     * @param pUser
     * @return
     */
    @PostMapping("/register/personal")
    public ResponseEntity pRegister(@RequestBody PUser pUser){
        return userService.addPersonalUser(pUser);
    }

    /**
     * 向指定号码发送一条验证码(4位，随机生成)
     * @param user
     * @return
     * get请求没有请求体 如果传递对象作为参数 会出现跨域错误
     */
    @PostMapping("/sendPhoneMsg")
    public String sendPhoneMsg(@RequestBody CUser user){
        //随机生成 4位验证码
        return phoneService.sendPhoneMsg(user.getPhone(),String.format("%04d",new Random().nextInt(9999)));
    }

    /**
     * 得到推荐资源
     * @param userId
     * @return
     */
    @GetMapping("/getRecommendationResources/{page}/{pageSize}")
    public ResponseEntity getRecommendationResources(@RequestHeader(value = "userId") String userId,@PathVariable Integer page,@PathVariable Integer pageSize){
        return workService.getTrueResourcesByOwnResourcesId(userId,page,pageSize);
    }

    /**
     * 得到已发布资源
     * @param userId
     * @return
     */
    @GetMapping("/getPublishedResources/{page}/{pageSize}")
    public ResponseEntity getPublishedResources(@RequestHeader(value = "userId") String userId,@PathVariable Integer page,@PathVariable Integer pageSize){
        return workService.getPublishedResources(userId,page,pageSize);
    }

    /**
     * 根据用户id，获取待匹配资源
     * @param userId
     * @return
     */
    @GetMapping("/getToBeMatchResources")
    public ResponseEntity getToBeMatchResources(@RequestHeader(value = "userId") String userId){
        return workService.getToBeMatchResources(userId);
    }

    /**
     * 修改flow_match表中matchStatus中的状态以及resources表中的status状态
     * @param flowMatchVo
     * @return
     */
    @PostMapping("/modifyMatchStatus")
    public ResponseEntity modifyMatchStatus(@RequestBody  FlowMatchVo flowMatchVo){
        return workService.modifyMatchStatus(flowMatchVo.getId(),flowMatchVo.getMatchStatus(),flowMatchVo.getAdvertiseId(),flowMatchVo.getFlowName());
    }

    /**
     * 向flow_match表中添加一条数据
     * @param flowMatchVo
     * @return
     */
    @PostMapping("/addFlowMatchInfo")
    public ResponseEntity addFlowMatchInfo(@RequestBody  FlowMatchVo flowMatchVo){
        return workService.addFlowMatchInfo(flowMatchVo);
    }

    /**
     * 查询置顶
     * @return
     */
    @PostMapping("/queryTop")
    public ResponseEntity queryTop(){
        return workService.queryTop();
    }
}
